package com.hmall.trade.listener;

import com.hmall.api.client.CartClient;
import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;
@Slf4j
@Component
@RequiredArgsConstructor
public class PayStatusListener {

    private final IOrderService orderService;
    private final CartClient  cartClient;

   @RabbitListener(bindings = @QueueBinding(
           value = @Queue(name = "trade.pay.success.queue",durable = "true"),
           exchange = @Exchange(name = "pay.direct"),
           key = "pay.success"
   ))
    public void listenPaySuccess(Long orderId){
        orderService.markOrderPaySuccess(orderId);
    }


    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = "trade.topic",type = ExchangeTypes.TOPIC),
            value = @Queue(name = "cart.clear.queue",durable = "true"),
            key = "order.create"
    ))
    public void listenCartClear(Map orderMap){
       log.info("订单创建成功，开始删除购物车数据");
        cartClient.deleteCartItemByIds((Collection<Long>) orderMap.get("itemIds"));
    }

}